// pages/cart/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        address: {},
        totalPrice: 0,
        count: 0,


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    //获取购物车信息
    getCartList() {
        let cart = wx.getStorageSync("goodCar");
        this.setData({
            cart
        })
    },
    //获取收货地址
    handlerChooseAddress() {
        wx.getSetting({
            success: (result) => {
                let address = result.authSetting["scope.address"]
                if (address === true || address === undefined) {
                    wx.chooseAddress({
                        success: (result1) => {
                            wx.setStorageSync("address", result1);
                        },
                    });

                } else {
                    wx.openSetting({
                        success: (result3) => {
                            wx.chooseAddress({
                                success: (result2) => {
                                    wx.setStorageSync("address", result2);
                                },
                            });
                        },
                    });

                }

            },
        });
    },

    //数量变换
    changeNum(e) {
        let { num, index } = e.target.dataset
        let cart = this.data.cart
        cart[index].num += parseInt(num)
        this.setData({
            cart
        })
        wx.setStorageSync("goodCar", cart)
        this.getCountPrice()
    },

    //选中取消
    checkboxChange(e) {
        let { index } = e.target.dataset
        let { cart } = this.data
        cart[index].checked = !cart[index].checked
        wx.setStorageSync("goodCar", cart);
        this.setData({
            cart
        })
        this.getCountPrice()

    },

    //合计
    getCountPrice() {
        let cart = this.data.cart || []
        let flag = 0
        let totalPrice = cart.reduce((pre, next) => {
            if (next.checked) {
                flag++
                return pre + next.goods_price * next.num
            } else {
                return pre
            }

        }, 0)
        this.setData({
            totalPrice,
            count: flag
        })
    },

    //取消全选
    cencalOrAll() {
        let { count, cart } = this.data
        if (count == cart.length) {
            cart.forEach(item => {
                item.checked = false
            })
            this.setData({
                cart,
            })
        } else {
            cart.forEach(item => {
                item.checked = true
            })
            this.setData({
                cart,
            })
        }
        wx.setStorageSync("goodCar", cart);
        this.getCountPrice()
    },

    //结算
    jiesuan() {
        let { address, count } = this.data
        if (!address) {
            wx.showToast({
                title: '未地址选择地址',
                icon: 'none',
                duration: 1500,
            });
            return
        }
        if (!count) {
            wx.showToast({
                title: '购物车为空',
                icon: 'none',
                duration: 1500,
            });
            return
        }
        wx.navigateTo({
            url: '/pages/pay/index',
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCartList()
        let address = wx.getStorageSync("address");

        this.setData({
            address,

        })
        this.getCountPrice()

        //判断是否有token

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})